<template>
    <div class="customer-student-card">
        <!-- 相交头像区域 -->
        <div class="avatar-container">


            <!-- 学生头像 (前景，如果有学生信息) -->
            <div v-if="hasStudent">
                <v-avatar v-if="!props.item.students[0].avatar" :src="props.item.students[0].avatar"
                    :color="studentAvatarColor" size="60" class="student-avatar">
                    <span class="text-white text-caption">{{ studentInitial }}</span>
                </v-avatar>
                <!-- 显示头像 -->
                <v-avatar v-else :size="60" class="student-avatar">
                    <v-img :src="props.item.students[0].avatar" cover></v-img>
                </v-avatar>
            </div>
            <div v-else>
                <!-- 客户头像 (背景) -->
                <v-avatar v-if="hasStudent && !props.item.students[0].avatar" :src="props.item.students[0].avatar"
                    :color="studentAvatarColor" size="60" class="student-avatar">
                    <span class="text-white text-caption">{{ studentInitial }}</span>
                </v-avatar>
                <!-- 显示头像 -->
                <v-avatar v-else :size="60" class="student-avatar">
                    <v-img :src="props.item.students[0].avatar" cover></v-img>
                </v-avatar>
            </div>


        </div>

        <!-- 信息显示区域 -->
        <div class="info-container ">
            <!-- 优先显示学生信息 -->
            <div v-if="hasStudent" class="primary-info">
                <div class="name-section">
                    <span class="primary-name">{{ studentName }}</span>
                    <!-- <v-chip v-if="isPrimary" color="primary" size="x-small" variant="outlined" class="ml-1">
                        <v-icon size="10" start>mdi-star</v-icon>
                        主要
                    </v-chip> -->
                </div>
                <div class="secondary-info">
                    <div class="contact-info">
                        <!-- <v-icon size="12" class="mr-1">mdi-phone</v-icon> -->
                        <span>{{ customerPhone || '无手机号' }}</span>
                    </div>
                    <div class="relation-info">
                        <span class="relation-label">{{ relationLabel }}</span>
                        <span class="customer-name">{{ customerName || '未设置' }}</span>
                    </div>
                </div>
            </div>

            <!-- 没有学生时显示客户信息 -->
            <div v-else class="primary-info">
                <div class="name-section">
                    <span class="primary-name">{{ customerName || '未设置' }}</span>
                </div>
                <div class="secondary-info">
                    <div class="contact-info">
                        <v-icon size="12" class="mr-1">mdi-phone</v-icon>
                        <span>{{ customerPhone || '无手机号' }}</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
import {  ref, reactive, computed, onMounted  } from 'vue';
import type { CustomerStudentRelationModel } from '@/api/customer/customerrelation/CustomerStudentRelationManagementApi';
import { getDictListSync } from '@/utils/dict';

interface Props {
    item: CustomerStudentRelationModel;
}
const dict = reactive({
    relation_type: [] as any[]
});


const props = defineProps<Props>();

onMounted(() => {
    dict.relation_type = getDictListSync('relation_type') || [];
});

// 计算属性
const hasStudent = computed(() => !!props.item.students[0].name);

const customerName = computed(() => props.item.customer.name);
const customerPhone = computed(() => props.item.customer.phone);
const customerInitial = computed(() => customerName.value?.charAt(0) || '客');

const studentName = computed(() => props.item.students[0].name);
const studentInitial = computed(() => studentName.value?.charAt(0) || '学');

const isPrimary = computed(() => props.item.isPrimary === '1');

const customerAvatarColor = computed(() => 'primary');
const studentAvatarColor = computed(() => 'success');

// 关系标签
const relationLabel = computed(() => {
    if (!dict || !props.item.relationType) return '';

    const relationTypes = dict['relation_type'] as any[];
    const relation = relationTypes?.find(item => item.dictValue === props.item.relationType);
    return relation?.dictName || '';
});
</script>

<style lang="scss" scoped>
.customer-student-card {
    display: flex;
    align-items: center;
    gap: 12px;
}

.avatar-container {
    width: 60px;
    height: 60px;
    flex-shrink: 0;
}

.customer-avatar {

    border: 2px solid rgba(255, 255, 255, 0.8);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.student-avatar {
    border: 3px solid rgba(255, 255, 255, 0.9);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    z-index: 2;
}

.info-container {
    flex: 1;
    min-width: 0; // 防止文字溢出
}

.primary-info {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.name-section {
    display: flex;
    align-items: center;
    gap: 4px;
}

.primary-name {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--v-primary-base);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.secondary-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
    font-size: 0.8rem;
    // color: rgba(0, 0, 0, 0.6);
}

.contact-info {
    display: flex;
    align-items: center;
    gap: 2px;
}

.relation-info {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 0.75rem;
}

.relation-label {
    background-color: rgba(25, 118, 210, 0.1);
    color: var(--v-primary-base);
    padding: 1px 6px;
    border-radius: 8px;
    font-size: 0.7rem;
    font-weight: 500;
}

.customer-name {
    color: var(--v-primary-base);
    font-size: 0.75rem;
}

// 响应式设计
@media (max-width: 768px) {
    .customer-student-card {
        gap: 8px;
    }

    .avatar-container {
        width: 44px;
        height: 36px;
    }

    .customer-avatar {
        width: 36px !important;
        height: 36px !important;
    }

    .student-avatar {
        width: 28px !important;
        height: 28px !important;
        top: 8px;
        left: 14px;
    }

    .primary-name {
        font-size: 0.9rem;
    }

    .secondary-info {
        font-size: 0.75rem;
    }
}
</style>